<template>
  <div class="article-edit-container">
    <el-card class="article-card">
      <template #header>
        <div class="card-header">
          <h2>编辑文章</h2>
        </div>
      </template>
      
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="10" animated />
      </div>
      
      <el-form
        v-else
        ref="articleFormRef"
        :model="articleForm"
        :rules="articleRules"
        label-position="top"
        @submit.prevent
      >
        <!-- 标题 -->
        <el-form-item label="文章标题" prop="title">
          <el-input
            v-model="articleForm.title"
            placeholder="请输入标题（5-50个字符）"
            maxlength="50"
            show-word-limit
          />
        </el-form-item>
        
        <!-- 板块选择 -->
        <el-form-item label="所属板块" prop="boardId">
          <el-select
            v-model="articleForm.boardId"
            placeholder="请选择板块"
            style="width: 100%"
            popper-append-to-body
          >
            <el-option
              v-for="board in boards"
              :key="board.id"
              :label="board.name"
              :value="board.id"
            />
          </el-select>
        </el-form-item>
        
        <!-- 标签 -->
        <el-form-item label="文章标签" prop="tags">
          <el-tag
            v-for="tag in dynamicTags"
            :key="tag"
            closable
            class="tag-item"
            @close="handleTagClose(tag)"
          >
            {{ tag }}
          </el-tag>
          
          <el-input
            v-if="tagInputVisible"
            ref="tagInputRef"
            v-model="tagInputValue"
            class="tag-input"
            size="small"
            @keyup.enter="handleTagInputConfirm"
            @blur="handleTagInputConfirm"
          />
          
          <el-button
            v-else
            class="tag-add-button"
            size="small"
            @click="showTagInput"
          >
            + 添加标签
          </el-button>
          
          <div class="tag-tip">最多添加5个标签，每个标签不超过10个字符</div>
        </el-form-item>
        
        <!-- 摘要 -->
        <el-form-item label="文章摘要" prop="summary">
          <el-input
            v-model="articleForm.summary"
            type="textarea"
            placeholder="请输入文章摘要（选填，最多200个字符）"
            :rows="3"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
        
        <!-- Markdown编辑器 -->
        <el-form-item label="文章内容" prop="content">
          <MdEditor
            v-model="articleForm.content"
            style="height: 500px"
            :preview="true"
            :toolbarsExclude="['github']"
            placeholder="请输入文章内容..."
            @onUploadImg="handleImgAdd"
            codeTheme="atom-one-dark"
            previewTheme="vuepress"
            :noMermaid="false"
            :noKatex="false"
            :config="{
              uploadImgShowBase64: false,
              uploadImgMaxSize: 10 * 1024 * 1024,
              markdownUrl: ''
            }"
          />
        </el-form-item>
        
        <!-- 提交按钮 -->
        <el-form-item>
          <div class="form-actions">
            <el-button @click="handleCancel">取消</el-button>
            <el-button type="primary" @click="handleSubmit" :loading="submitting">保存修改</el-button>
            <el-button type="info" @click="handleDraft">保存为草稿</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, computed, nextTick, onMounted, onBeforeUnmount } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/api'
import { Plus } from '@element-plus/icons-vue'
import MdEditor from 'md-editor-v3'
import 'md-editor-v3/lib/style.css'
import axios from 'axios'

export default {
  name: 'ArticleEdit',
  components: {
    Plus,
    MdEditor
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    
    const tagInputRef = ref(null)
    const loading = ref(true)
    const submitting = ref(false)
    
    // 获取文章ID
    const articleId = computed(() => route.params.id)
    
    // 表单数据
    const articleForm = reactive({
      id: '',
      title: '',
      boardId: '',
      tags: '',
      summary: '',
      content: '',
      html: '',
      isDraft: false
    })
    
    // 表单验证规则
    const articleRules = {
      title: [
        { required: true, message: '请输入文章标题', trigger: 'blur' },
        { min: 5, max: 50, message: '标题长度应在5到50个字符之间', trigger: 'blur' }
      ],
      boardId: [
        { required: true, message: '请选择板块', trigger: 'change' }
      ],
      content: [
        { required: true, message: '请输入文章内容', trigger: 'blur' },
        { min: 10, message: '内容太短，至少10个字符', trigger: 'blur' }
      ]
    }
    
    // 板块列表
    const boards = computed(() => store.state.boards || [])
    
    // 标签相关
    const dynamicTags = ref([])
    const tagInputVisible = ref(false)
    const tagInputValue = ref('')
    
    // 上传相关
    const uploadHeaders = computed(() => {
      const token = localStorage.getItem('token')
      return { Authorization: token }
    })
    
    // 加载文章详情
    const loadArticleDetail = async () => {
      loading.value = true
      
      try {
        const response = await api.article.getDetail(articleId.value)
        const article = response.data
        
        // 填充表单数据
        articleForm.id = article.id
        articleForm.title = article.title
        articleForm.boardId = article.boardId
        articleForm.summary = article.summary || ''
        articleForm.content = article.content
        
        // 处理标签
        if (article.tags) {
          const tags = typeof article.tags === 'string' 
            ? article.tags.split(',').filter(tag => tag.trim())
            : article.tags
          
          dynamicTags.value = tags
          articleForm.tags = tags.join(',')
        }
        
        // 检查当前用户是否有权限编辑文章
        const currentUserId = store.getters['user/userId'];
        if (!currentUserId || (article.userId !== currentUserId && !store.getters['user/isAdmin'])) {
          ElMessage.error('您没有权限编辑此文章');
          router.push(`/article/${articleId.value}`);
          return;
        }
      } catch (error) {
        console.error('获取文章详情失败:', error)
        ElMessage.error('获取文章详情失败，请稍后重试')
        router.push('/articles')
      } finally {
        loading.value = false
      }
    }
    
    // 方法
    const showTagInput = () => {
      if (dynamicTags.value.length >= 5) {
        ElMessage.warning('最多添加5个标签')
        return
      }
      
      tagInputVisible.value = true
      nextTick(() => {
        tagInputRef.value.focus()
      })
    }
    
    const handleTagInputConfirm = () => {
      const inputValue = tagInputValue.value.trim()
      
      if (inputValue && !dynamicTags.value.includes(inputValue)) {
        if (inputValue.length > 10) {
          ElMessage.warning('标签长度不能超过10个字符')
        } else {
          dynamicTags.value.push(inputValue)
          // 更新表单中的tags字段
          articleForm.tags = dynamicTags.value.join(',')
        }
      }
      
      tagInputVisible.value = false
      tagInputValue.value = ''
    }
    
    const handleTagClose = (tag) => {
      dynamicTags.value = dynamicTags.value.filter(t => t !== tag)
      // 更新表单中的tags字段
      articleForm.tags = dynamicTags.value.join(',')
    }
    
    // 处理编辑器图片上传
    const handleImgAdd = async (files, insertCallback) => {
      try {
        console.log('开始上传图片...');
        
        if (!files || files.length === 0) return;
        
        const file = files[0];
        const formData = new FormData();
        formData.append('file', file);
        
        // 获取token，用于认证
        const token = localStorage.getItem('token');
        if (!token) {
          ElMessage.error('请先登录后再上传图片');
          return;
        }
        
        // 直接使用axios实例进行上传
        const response = await axios.post(
          'http://localhost:5855/upload/image', 
          formData, 
          {
            params: { token },
            headers: { 'Content-Type': 'multipart/form-data' }
          }
        );
        
        console.log('上传结果:', response);
        
        if (response.data && response.data.code === 0) {
          // 从message中获取图片URL
          const imageUrl = response.data.message;
          
          // 增加空值检查
          if (!imageUrl) {
            ElMessage.error('图片上传成功但未返回有效的URL');
            return;
          }
          
          console.log('后端返回的图片URL:', imageUrl);
          
          // 关键修改: md-editor-v3要求回调函数接收URL数组，而不是Markdown文本
          // 这是最符合其API规范的做法
          if (typeof insertCallback === 'function') {
            console.log('使用URL数组调用insertCallback:', [imageUrl]);
            
            // 确保URL是绝对路径，如果不是绝对路径，浏览器可能无法解析
            const urlsToInsert = [imageUrl];
            
            // 直接调用回调函数，传入URL数组
            insertCallback(urlsToInsert);
            
            ElMessage.success('图片上传成功');
          } else {
            console.error('insertCallback不是函数:', typeof insertCallback);
            ElMessage.warning('图片上传成功，但无法插入到编辑器');
          }
        } else {
          ElMessage.error('图片上传失败: ' + (response.data?.message || '未知错误'));
        }
      } catch (error) {
        console.error('上传图片出错:', error);
        ElMessage.error('图片上传失败：' + (error.message || '未知错误'));
      }
    };
    
    // Simplify this function since we don't need it anymore
    const convertMarkdownToHtml = () => {
      return ''; // We're now using MdEditor's built-in functionality
    };
    
    // 处理提交
    const handleSubmit = () => {
      // 不依赖表单验证，手动验证必填字段
      if (!articleForm.title.trim()) {
        ElMessage.warning('请输入文章标题')
        return
      }
      
      if (articleForm.title.trim().length < 5 || articleForm.title.trim().length > 50) {
        ElMessage.warning('标题长度应在5到50个字符之间')
        return
      }
      
      if (!articleForm.boardId) {
        ElMessage.warning('请选择板块')
        return
      }
      
      if (!articleForm.content.trim()) {
        ElMessage.warning('文章内容不能为空')
        return
      }
      
      if (articleForm.content.trim().length < 10) {
        ElMessage.warning('内容太短，至少10个字符')
        return
      }
      
      submitting.value = true
      articleForm.isDraft = false
      
      // 清除所有可能存在的消息
      ElMessage.closeAll()
      
      // 准备要发送的数据
      const updateData = {
        id: articleId.value,
        title: articleForm.title,
        content: articleForm.content,
        boardId: articleForm.boardId,
        tags: articleForm.tags,
        summary: articleForm.summary
      }
      
      console.log('提交文章修改, 数据:', JSON.stringify(updateData))
      
      api.article.update(updateData)
        .then(response => {
          if (response && response.code === 0) {
            ElMessage.success('文章更新成功')
            router.push(`/article/${articleId.value}`)
          } else {
            // 后端返回了响应但有错误码
            console.error('更新文章失败:', response)
            ElMessage.error(response.message || '更新失败，请稍后重试')
          }
        })
        .catch(error => {
          console.error('更新文章失败:', error)
          ElMessage.error(error.message || '更新失败，请稍后重试')
        })
        .finally(() => {
          submitting.value = false
        })
    }
    
    // 保存草稿
    const handleDraft = () => {
      if (!articleForm.title.trim()) {
        ElMessage.warning('请先填写文章标题')
        return
      }
      
      articleForm.isDraft = true
      submitting.value = true
      
      // 清除所有可能存在的消息
      ElMessage.closeAll()
      
      // 准备要发送的数据
      const draftData = {
        id: articleId.value,
        title: articleForm.title,
        content: articleForm.content,
        boardId: articleForm.boardId,
        tags: articleForm.tags,
        summary: articleForm.summary,
        isDraft: true
      }
      
      console.log('保存文章为草稿, 数据:', JSON.stringify(draftData))
      
      api.article.update(draftData)
        .then(response => {
          if (response && response.code === 0) {
            ElMessage.success('草稿保存成功')
          } else {
            // 后端返回了响应但有错误码
            console.error('保存草稿失败:', response)
            ElMessage.error(response.message || '保存失败，请稍后重试')
          }
        })
        .catch(error => {
          console.error('保存草稿失败:', error)
          ElMessage.error(error.message || '保存失败，请稍后重试')
        })
        .finally(() => {
          submitting.value = false
        })
    }
    
    // 取消编辑
    const handleCancel = () => {
      ElMessageBox.confirm('确定要放弃编辑吗？未保存的修改将丢失', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        router.push(`/article/${articleId.value}`)
      }).catch(() => {})
    }
    
    onMounted(() => {
      // 加载板块数据
      if (boards.value.length === 0) {
        store.dispatch('fetchBoards')
      }
      
      // 加载文章详情
      loadArticleDetail()
    })
    
    // 组件卸载前清理资源
    onBeforeUnmount(() => {
      // 清理可能的观察者
      if (window.ResizeObserver) {
        // 强制完成所有挂起的观察者通知
        window.requestAnimationFrame(() => {
          window.requestAnimationFrame(() => {
            // 双重RAF调用通常可以解决观察者循环问题
          })
        })
      }
    })
    
    return {
      loading,
      articleForm,
      articleRules,
      boards,
      dynamicTags,
      tagInputVisible,
      tagInputValue,
      tagInputRef,
      uploadHeaders,
      submitting,
      showTagInput,
      handleTagInputConfirm,
      handleTagClose,
      handleImgAdd,
      handleSubmit,
      handleDraft,
      handleCancel,
      convertMarkdownToHtml
    }
  }
}
</script>

<style scoped>
.article-edit-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
}

.loading-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h2 {
  margin: 0;
  font-size: 24px;
  color: #303133;
}

.tag-item {
  margin-right: 8px;
  margin-bottom: 8px;
}

.tag-input {
  width: 100px;
  margin-right: 8px;
  vertical-align: bottom;
}

.tag-add-button {
  margin-bottom: 8px;
}

.tag-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.cover-uploader {
  width: 100%;
}

.cover-image {
  width: 100%;
  max-height: 300px;
  object-fit: cover;
  border-radius: 4px;
}

.cover-placeholder {
  width: 100%;
  height: 180px;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: border-color 0.3s;
}

.cover-placeholder:hover {
  border-color: #409EFF;
}

.cover-placeholder .el-icon {
  font-size: 28px;
  color: #8c939d;
}

.upload-text {
  color: #8c939d;
  margin-top: 10px;
  font-size: 14px;
}

@media screen and (max-width: 768px) {
  .article-edit-container {
    padding: 10px;
  }
  
  .form-actions {
    flex-direction: column;
    gap: 10px;
  }
  
  .form-actions .el-button {
    width: 100%;
  }
}

/* Code syntax highlighting styles - For consistency with ArticleDetail.vue */
:deep(.code-block-container) {
  margin: 20px 0;
  border-radius: 6px;
  overflow: hidden;
  font-family: 'SFMono-Regular', Consolas, Monaco, 'Andale Mono', monospace;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

:deep(.code-block-header) {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #1e1e1e;
  color: #abb2bf;
  padding: 8px 16px;
  height: 36px;
  position: relative;
}

:deep(.code-block-dots) {
  display: flex;
  gap: 8px;
}

:deep(.dot) {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

:deep(.dot.red) {
  background-color: #ff5f56;
}

:deep(.dot.yellow) {
  background-color: #ffbd2e;
}

:deep(.dot.green) {
  background-color: #27c93f;
}

:deep(.code-language) {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  color: #abb2bf;
  font-size: 13px;
  text-transform: lowercase;
}

:deep(.code-copy-button) {
  cursor: pointer;
  color: #abb2bf;
  font-size: 14px;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

:deep(.code-copy-button:hover) {
  background-color: #2c313a;
  color: #ffffff;
}

:deep(.code-block-body) {
  background-color: #1e1e1e;
  padding: 0;
  margin: 0;
  overflow-x: auto;
}

:deep(.code-content-with-lines) {
  counter-reset: line;
  font-family: 'SFMono-Regular', Consolas, Monaco, 'Andale Mono', monospace;
  background-color: transparent;
  color: #abb2bf;
  font-size: 14px;
  line-height: 1.6;
  padding: 10px 0;
  white-space: pre;
  word-break: normal;
  tab-size: 2;
}

:deep(.code-line) {
  position: relative;
  display: flex;
  border-left: 3px solid transparent;
  min-height: 20px;
}

:deep(.code-line:hover) {
  background-color: rgba(255, 255, 255, 0.1);
  border-left: 3px solid #409EFF;
}

:deep(.line-number) {
  user-select: none;
  text-align: right;
  color: #636d83;
  min-width: 40px;
  padding-right: 16px;
  padding-left: 8px;
  font-size: 0.85em;
}

:deep(.line-content) {
  flex: 1;
  padding-right: 16px;
  white-space: pre;
}

:deep(.inline-code) {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
  background-color: rgba(175, 184, 193, 0.2);
  border-radius: 3px;
  padding: 0.2em 0.4em;
  font-size: 85%;
  color: #24292f;
}

:deep(.hljs-special-class) {
  color: #ff8400 !important;
  font-weight: bold;
  background-color: rgba(255, 132, 0, 0.07);
  padding: 1px 2px;
  border-radius: 2px;
}
</style> 